package com.yunzheng.studyLeaveSystem.modules.user.user_info.dao;

import com.yunzheng.studyLeaveSystem.modules.user.user_info.entity.User_info;
import com.yunzheng.studyLeaveSystem.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: YunZheng
 * @Date: 2023/01/30/10:52
 * @Description:
 */
public class User_infoDao {


    /**
     * 通过登录的电话查询用户信息
     *
     * @param phone_num
     * @return
     */
    public User_info getUser_info(String phone_num) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "select * from user_info where phone_num =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, phone_num);
        ResultSet resultSet = preparedStatement.executeQuery();
        User_info user_info = null;
        while (resultSet.next()) {
            String phone_num1 = resultSet.getString(1);
            String password = resultSet.getString(2);
            String username = resultSet.getString(3);
            String gender = resultSet.getString(4);
            String term = resultSet.getString(5);
            String direction = resultSet.getString(6);
            String signature = resultSet.getString(7);
            String hometown = resultSet.getString(8);
            String constellation = resultSet.getString(9);
            String birthday = resultSet.getString(10);
            String QQ = resultSet.getString(11);
            String weixin = resultSet.getString(12);
            String email = resultSet.getString(13);
            user_info = new User_info(phone_num1, password, username, gender, term, direction, signature,hometown, constellation, birthday, QQ, weixin, email);
        }
        DBUtils.release(resultSet, preparedStatement, connection);
        return user_info;
    }

    /**
     * 修改个人信息
     * @param user_info
     * @param phone_num
     * @throws Exception
     */
    public void update(User_info user_info, String phone_num) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "update user_info set gender=?,set direction=?,set term=?,set hometown=?,set constellation=?,set birthday=?,set signature=? where phone_num=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(2, phone_num);
        int rows = preparedStatement.executeUpdate();
        DBUtils.release(null,preparedStatement, connection);
    }

    /**
     * 修改密码
     * @param password
     * @param phone_num
     * @throws Exception
     */
    public void updatePassword(String password, String phone_num) throws Exception {
        Connection connection = DBUtils.getCon();
        String sql = "update user_info set password=? where phone_num=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, password);
        preparedStatement.setString(2, phone_num);
        int rows = preparedStatement.executeUpdate();
        DBUtils.release(null,preparedStatement, connection);
    }

    /**
     * 查询密码
     * @param phone_num
     * @return
     * @throws Exception
     */
    public String queryPassword(String phone_num) throws Exception {
        String password = null;
        Connection connection = DBUtils.getCon();
        String sql = "select password from user_info where phone_num = ?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,phone_num);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            password = resultSet.getString("password");
        }
        DBUtils.release(resultSet,preparedStatement,connection);
        return password;
    }


}
